package com.atguigu.gulimallcart.controller;

import com.atguigu.gulimallcart.interceptor.CartInterceptor;
import com.atguigu.gulimallcart.service.CartService;
import com.atguigu.gulimallcart.to.UserInfoTo;
import com.atguigu.gulimallcart.vo.Cart;
import com.atguigu.gulimallcart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @ClassName CartController
 * @Description TODO
 * @Detail detail
 * @Author MyPC
 * @Date 2020/7/13
 * @Version 1.0
 */
@Controller
public class CartController {
    @Autowired
    CartService cartService;

    @GetMapping("/getCurrentUserCartItems")
    @ResponseBody  //返回Json数据要标注该注解
    public List<CartItem> getCurrentUserCartItems(){
        return cartService.getCurrentUserCartItems();
    }

    /**
     * 根据商品id删除购物车商品数据
     * @param skuId
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("") Long skuId){
        cartService.deleteItem(skuId);
        //重定向到列表页
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 实时改变商品数量
     * @param skuId
     * @param count
     * @return
     */
    @GetMapping("/changeCount")
    public String changeCount(@RequestParam("skuId") Long skuId,@RequestParam("count") Integer count){
        cartService.changeCount(skuId,count);
        //重定向到列表页
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 实时改变商品的选择状态
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping("/changeCheckStatus")
    public String changeCheckStatus(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check){
        cartService.changeCheckStatus(skuId,check);
        //重定向到列表页
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 获取购物车列表
     * @return
     */
    @GetMapping("cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        Cart cart=cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }

    /**
     * 加入购物车
     * @return
     */
    @GetMapping("addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num,
                            RedirectAttributes redirect) throws ExecutionException, InterruptedException {
        //添加购物车
        cartService.addToCart(skuId,num);
        //redirect.addFlashAttribute()  添加的数据只能在页面使用异一次，页面刷新则清除
        //redirect.addAttribute("skuId",skuId);属性会自动添加在重定向地址的后面
        redirect.addAttribute("skuId",skuId);
        //重定向到成功页面，防止页面刷新又再一次添加购物车
        return "redirect:http://cart.gulimall.com/addCartSuccess.html";
    }

    /**
     * 添加购物车成功展示的页面
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("addCartSuccess.html")
    public String addCartSuccessPage(@RequestParam("skuId") Long skuId,Model model){
        //根据SkuId查询购物车信息
        CartItem cartItem=cartService.getCartInfoBySkuId(skuId);
        model.addAttribute("item",cartItem);
        return "success";
    }

}
